/**
 * 
 */
package pt.mleiria.sAction.vo.chart;

import java.sql.Date;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;

import pt.mleiria.sAction.utils.DateUtil;

/**
 * @author Manuel
 *
 */
public class ChartVO {
	Log log = LogFactory.getLog(ChartVO.class);
	private final Date[] xSeries;
	//private final String[] seriesNames;
	private final TimeSeriesCollection dataset;
	private final Map<String, Double[]> seriesNameAndValues;
	private final String chartTitle;
	private final String xAxisLabel;
	private final String yAxisLabel;
	/**
	 * 
	 * @param chartTitle
	 * @param xAxisLabel
	 * @param yAxisLabel
	 * @param seriesNameAndValues
	 * @param xSeries
	 */
	public ChartVO(final String chartTitle, final String xAxisLabel, final String yAxisLabel,
			final Map<String, Double[]> seriesNameAndValues, 
			final Date[] xSeries) {
		this.chartTitle = chartTitle;
		this.xAxisLabel = xAxisLabel;
		this.yAxisLabel = yAxisLabel;
		this.seriesNameAndValues = seriesNameAndValues;
		this.xSeries = xSeries;
		
		this.dataset = new TimeSeriesCollection(){
			private static final long serialVersionUID = 1L;

			/**
			 * @see java.lang.Object#finalize()
			 */
			protected void finalize() throws Throwable {
				super.finalize();
				log.info(this +" finalized.");
			}
    	};
		processChart();
	}
	private void processChart(){
		try {
			Iterator<Map.Entry<String, Double[]>> it = seriesNameAndValues.entrySet().iterator();
			while (it.hasNext()) {
				Map.Entry<String, Double[]> entry = it.next();
				String serieName = entry.getKey();
				Double[] seriesValues = entry.getValue();
				int dataLength = seriesValues.length;
				if (dataLength != xSeries.length)
					throw new IllegalArgumentException("Os pontos no eixo xx ["
							+ xSeries
							+ "] tem de ter o mesmo cmprimento dos em yy ["
							+ seriesValues + "]");
				TimeSeries ts = new TimeSeries(serieName);
				for (int i = 0, n = dataLength; i < n; i++) {
					//dataset.addValue(seriesValues[i], serieName, xSeries[i]);
					ts.add(new Day(xSeries[i]),seriesValues[i]);
					log.info("DATASET(" + seriesValues[i] + "," + serieName	+ "," + xSeries[i] + ")");
				}
				dataset.addSeries(ts);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	/**
	 * @return the xSeries
	 */
	public Date[] getXSeries() {
		return xSeries;
	}
	/**
	 * @return the dataset
	 */
	public TimeSeriesCollection getDataset() {
		return dataset;
	}
	/**
	 * @return the seriesNameAndValues
	 */
	public Map<String, Double[]> getSeriesNameAndValues() {
		return seriesNameAndValues;
	}
	/**
	 * @return the chartTitle
	 */
	public String getChartTitle() {
		return chartTitle;
	}
	/**
	 * @return the xAxisLabel
	 */
	public String getXAxisLabel() {
		return xAxisLabel;
	}
	/**
	 * @return the yAxisLabel
	 */
	public String getYAxisLabel() {
		return yAxisLabel;
	}
}
